/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.usuario.modelo;

/**
 *
 * @author francisco
 */
import java.security.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AlgoritmoAES {

    private byte[] keyValue;

    public AlgoritmoAES(byte[] password) {
        keyValue = password;
    }
    private static String ALGORITHM = "AES";
    private final String CODIFICACION = "UTF-8";

    public String encriptar(String valorEncriptar) {
        String valorEncriptado = null;
        try {
            Key clave = generarClave();
            Cipher c = Cipher.getInstance(ALGORITHM);
            c.init(Cipher.ENCRYPT_MODE, clave);
            byte[] encValor = c.doFinal(valorEncriptar.getBytes(CODIFICACION));
            valorEncriptado = HexToString(encValor);
        } catch (Exception ex) {
            Logger.getLogger(AlgoritmoAES.class.getName()).log(Level.SEVERE, null, ex);
        }
        return valorEncriptado;
    }

    private Key generarClave() throws Exception {
        Key clave = new SecretKeySpec(keyValue, ALGORITHM);
        return clave;
    }

    private String HexToString(byte[] arregloEncriptado) {
        String textoEncriptado = "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < arregloEncriptado.length; i++) {
            String hex = Integer.toHexString(0xff & arregloEncriptado[i]);
            if (hex.length() == 1) {
                sb.append(0X0); 
            }
            sb.append(hex);
        }
        textoEncriptado = sb.toString();
        return textoEncriptado;
    }
}
